package com.welchapps.nporganizer;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;


public class EditorActivity extends FragmentActivity {
	private static final String TAG = "EditorActivity";

    // The different distinct states the activity can be run in.
    private static final int STATE_EDIT = 0;
    private static final int STATE_INSERT = 1;
	
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    public static int mState;
    
    public static int ItemId;
    
    private static Uri mUri;
	private static Cursor mCursor;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editor_viewpager);
        final Intent intent = getIntent();
        
        final String action = intent.getAction();
        if (Intent.ACTION_EDIT.equals(action)) {
            // Requested to edit: set that state, and the data being edited.
            mState = STATE_EDIT;
//            Uri.Builder UriBuilder = new Uri.Builder();
//            UriBuilder = intent.getData().buildUpon();
//            UriBuilder.path("/notes/");
            ItemId = (int) getIntent().getLongExtra("item",0) - 1;
//            mUri =  ContentUris.withAppendedId((Uri) getIntent().getParcelableExtra("uri"), ItemId) ;
        } else if (Intent.ACTION_INSERT.equals(action)) {
            // Requested to insert: set that state, and create a new entry
            // in the container.
            mState = STATE_INSERT;
            setTitle(getText(R.string.title_create));
//			mUri = getContentResolver().insert(intent.getData(), null);
            // If we were unable to create a new note, then just finish
            // this activity.  A RESULT_CANCELED will be sent back to the
            // original activity if they requested a result.
//            if (mUri == null) {
//                Log.e(TAG, "Failed to insert new note into " + getIntent().getData());
//                finish();
//                return;
//            }
//
//            // The new entry was created, so assume all will end well and
//            // set the result to be returned.
//            setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));

        } else {
            // Whoops, unknown action!  Bail.
            Log.e(TAG, "Unknown action, exiting");
            finish();
            return;
        }
        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
//        mCursor = managedQuery(mUri, PROJECTION, null, null, null);
        mPagerAdapter = new EditorAdapter(getFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setCurrentItem(ItemId);
        setTitle("");
    }

//    @Override
//    public void onBackPressed() {
//        if (mPager.getCurrentItem() == 0) {
//            // If the user is currently looking at the first step, allow the system to handle the
//            // Back button. This calls finish() on this activity and pops the back stack.
//            super.onBackPressed();
//        } else {
//            // Otherwise, select the previous step.
//            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
//        }
//    }

    private class EditorAdapter extends FragmentStatePagerAdapter {
        public EditorAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return EditorFragment.create(position);
        }

        @Override
        public int getCount() {
        	if (mState == STATE_INSERT)
        		return 1;
        	else
        		return NotePad.objects.size() - 1;
//        	return 100;
        }
    }
    
}